package com.sanzang.util;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.sanzang.vo.JDBook;

public class HtmlParseUtil {

	public static void main(String[] args) throws Exception {
		//HtmlParseUtil.parseJD("java").forEach(System.out::println);
		HtmlParseUtil.lolHeads();
	}
	
	public static void lolHeads()  throws Exception {
		String url = "https://lol.qq.com/data/info-heros.shtml";
		Document doc = Jsoup.parse(new URL(url), 3000);
		System.out.println( doc.html());
		
	}

	public static List<JDBook> parseJD(String key) throws Exception {
		String url = "https://search.jd.com/Search?keyword=" + key;
		Document doc = Jsoup.parse(new URL(url), 3000);
		
		Element div = doc.getElementById("J_goodsList");
		Elements lis = div.getElementsByTag("li"); // 所有书节点
		if (lis == null) {
			System.out.println(" 没找到");
			return null;
		}
//		
		List<JDBook> list =new ArrayList<JDBook>();
		for (Element li : lis) {
			String img = li.getElementsByTag("img").get(0).attr("src");
			String price = li.getElementsByClass("p-price").get(0).text();
			String title = li.getElementsByClass("p-name").get(0).text();
			list.add(new JDBook(img,title,price));		
		}
		
		return list;
	}

}
